package com.yjdb.sanyuantang.modules.Admin.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yjdb.sanyuantang.aop.OperationLogAnnotation;
import com.yjdb.sanyuantang.common.result.CommonResult;
import com.yjdb.sanyuantang.common.result.ResultCode;
import com.yjdb.sanyuantang.modules.Admin.DTO.role.RoleDTO;
import com.yjdb.sanyuantang.modules.Admin.service.AdminRoleService;
import com.yjdb.sanyuantang.modules.Admin.DTO.role.RoleListDTO;
import com.yjdb.sanyuantang.modules.Admin.DTO.role.RolePage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * packageName com.yjdb.sanyuantang.modules.Admin.controller
 *
 * @author 混沌酱
 * @version 1.0
 * @className AdminRoleController
 * @date 2024/3/14
 * @description TODO
 */
@RestController
@RequestMapping("/admin/role")
@Tag(name = "后台角色管理")
public class AdminRoleController {

    private static final Logger logger = LoggerFactory.getLogger(AdminRoleController.class);

    @Resource
    private AdminRoleService adminRoleService;

    @OperationLogAnnotation(operModule = "后台角色管理", operFunction = "添加角色")
    @Operation(summary = "添加角色", description = "添加角色")
    @RequestMapping(path = "/add", method = RequestMethod.POST)
    public CommonResult<?> add(@RequestBody RoleDTO roleDTO) {
        try {
            adminRoleService.add(roleDTO);
            return CommonResult.success(ResultCode.SUCCESS,"角色:"+ roleDTO.getName()+"添加成功");
        } catch (Exception e) {
            logger.debug("添加失败", e);
            return CommonResult.failed(e.getMessage());
        }
    }

    @OperationLogAnnotation(operModule = "后台角色管理", operFunction = "删除角色")
    @Operation(summary = "删除角色", description = "删除角色")
    @RequestMapping(path = "/delete", method = RequestMethod.GET)
    public CommonResult<?> delete(
            @Parameter(name = "id",description = "角色id",required = true)  Integer id,
            @Parameter(name = "name",description = "角色名称",required = true)  String name
    ) {
        try {
            adminRoleService.delete(id,name);
            return CommonResult.success(ResultCode.SUCCESS,"角色:"+ name+"删除成功");
        } catch (Exception e) {
            logger.debug("删除失败", e);
            return CommonResult.failed(e.getMessage());
        }
    }

    @OperationLogAnnotation(operModule = "后台角色管理", operFunction = "修改角色")
    @Operation(summary = "修改角色", description = "修改角色")
    @RequestMapping(path = "/update", method = RequestMethod.POST)
    public CommonResult<?> update(@RequestBody RoleDTO roleDTO) {
        try {
            adminRoleService.update(roleDTO);
            return CommonResult.success(ResultCode.SUCCESS,"角色:"+ roleDTO.getName()+"修改成功");
        } catch (Exception e) {
            logger.debug("修改失败", e);
            return CommonResult.failed(e.getMessage());
        }
    }

    // 查询用户列表 分页查询
    @Operation(summary = "查询角色列表", description = "所提供字段都可以模糊查询,空字符代表全部")
    @RequestMapping(path = "/list", method = RequestMethod.POST)
    public CommonResult<PageInfo<RoleListDTO>> list(@RequestBody RolePage rolePage) {
        try {
            PageHelper.startPage(rolePage.getPageNum(), rolePage.getPageSize());
            List<RoleListDTO> list = adminRoleService.list(rolePage);
            PageInfo<RoleListDTO> listPage = new PageInfo<>(list);
            return CommonResult.success(listPage);
        } catch (Exception e) {
            logger.debug("查询失败", e);
            return CommonResult.failed(e.getMessage());
        }
    }

    // 启用或者关闭
    @OperationLogAnnotation(operModule = "后台角色管理", operFunction = "启用或者关闭")
    @Operation(summary = "启用或者关闭", description = "启用或者关闭")
    @RequestMapping(path = "/enable", method = RequestMethod.GET)
    public CommonResult<?> enable(
            @Parameter(name = "id",description = "角色id",required = true)  Integer id,
            @Parameter(name = "name",description = "角色名称",required = true)  String name,
            @Parameter(name = "status",description = "0表示关闭，1表示启用",required = true)  String status
    ) {
        try {
            adminRoleService.enable(id,name,status);
            return CommonResult.success(ResultCode.SUCCESS,"角色:"+ name+"启用或关闭成功");
        } catch (Exception e) {
            logger.debug("启用或关闭失败", e);
            return CommonResult.failed(e.getMessage());
        }
    }

    // 给角色分配权限
    @OperationLogAnnotation(operModule = "后台角色管理", operFunction = "分配权限")
    @Operation(summary = "分配权限", description = "分配权限")
    @RequestMapping(path = "/assign", method = RequestMethod.GET)
    public CommonResult<?> assign(
            @Parameter(name = "id",description = "角色id",required = true)  Integer id,
            @Parameter(name = "name",description = "角色名称",required = true)  String name,
            @Parameter(name = "permissionIds",description = "权限id集合用逗号隔开(空字符串表示解绑所有权限)",required = true)  String permissionIds
    ) {
        try {
            adminRoleService.assign(id,permissionIds);
            return CommonResult.success(ResultCode.SUCCESS,"角色:"+ name+"分配权限成功");
        } catch (Exception e) {
            logger.debug("分配权限失败", e);
            return CommonResult.failed(e.getMessage());
        }
    }


}
